import { BlogItem } from './mock';

export const generateBlogImage = (blog: BlogItem): string => {
  const canvas = document.createElement('canvas');
  canvas.width = 180;
  canvas.height = 120;
  const ctx = canvas.getContext('2d');

  if (!ctx) return '';

  // 创建渐变背景
  const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
  gradient.addColorStop(0, '#001529');
  gradient.addColorStop(1, '#003a8c');
  ctx.fillStyle = gradient;
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  // 设置文字样式
  ctx.font = 'bold 36px Arial';
  ctx.fillStyle = '#fff';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';

  // 绘制主题文字
  ctx.fillText(blog.theme, canvas.width / 2, canvas.height / 2);

  // 返回base64格式图片
  return canvas.toDataURL('image/jpeg');
};